home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n4.arc / L2.ASM < prev    next >
Assembly Source File  |  1990-09-13  |  4KB  |  139 lines

  1.  
  2. ; *** Listing 2 ***
  3. ;
  4. ; Program to illustrate searching through a buffer of a specified
  5. ; length until either a specified byte or a zero byte is encountered.
  6. ; A loop unrolled four times and terminated with LOOP is used.
  7.  
  8.     .model    small
  9.     .stack    100h
  10.     .data
  11. ; Sample string to search through.
  12. SampleString    label    byte
  13.     db    'This is a sample string of a long enough length '
  14.     db    'so that raw searching speed can outweigh any '
  15.     db    'extra set-up time that may be required.',0
  16. SAMPLE_STRING_LENGTH    equ    $-SampleString
  17.  
  18. ; User prompt.
  19. Prompt    db    'Enter character to search for:$'
  20.  
  21. ; Result status messages.
  22. ByteFoundMsg    db    0dh,0ah
  23.         db    'Specified byte found.',0dh,0ah,'$'
  24. ZeroByteFoundMsg db    0dh,0ah
  25.         db    'Zero byte encountered.',0dh,0ah,'$'
  26. NoByteFoundMsg    db    0dh,0ah
  27.         db    'Buffer exhausted with no match.',0dh,0ah,'$'
  28.  
  29. ; Table of initial, possibly partial loop entry points for
  30. ; SearchMaxLength.
  31. SearchMaxLengthEntryTable    label    word
  32.     dw    SearchMaxLengthEntry4
  33.     dw    SearchMaxLengthEntry1
  34.     dw    SearchMaxLengthEntry2
  35.     dw    SearchMaxLengthEntry3
  36.  
  37.     .code
  38. Start    proc    near
  39.     mov    ax,@data    ;point to standard data segment
  40.     mov    ds,ax
  41.  
  42.     mov    dx,offset Prompt
  43.     mov    ah,9        ;DOS print string function
  44.     int    21h        ;prompt the user
  45.  
  46.     mov    ah,1        ;DOS get key function
  47.     int    21h        ;get the key to search for
  48.  
  49.     mov    ah,al        ;put character to search for in AH
  50.     mov    cx,SAMPLE_STRING_LENGTH ;# of bytes to search
  51.     mov    si,offset SampleString ;point to buffer to search
  52.     call    SearchMaxLength    ;search the buffer
  53.     mov    dx,offset ByteFoundMsg ;assume we found the byte
  54.     jc    PrintStatus    ;we did find the byte
  55.                 ;we didn't find the byte, figure out
  56.                 ; whether we found a zero byte or
  57.                 ; ran out of buffer
  58.     mov    dx,offset NoByteFoundMsg
  59.                 ;assume we didn't find a zero byte
  60.     jcxz    PrintStatus    ;we didn't find a zero byte
  61.     mov    dx,offset ZeroBytefoundMsg ;we found a zero byte
  62. PrintStatus:
  63.     mov    ah,9        ;DOS print string function
  64.     int    21h        ;report status
  65.  
  66.     mov    ah,4ch        ;return to DOS
  67.     int    21h
  68. Start    endp
  69.  
  70. ; Function to search a buffer of a specified length until either a
  71. ; specified byte or a zero byte is encountered.
  72. ;
  73. ; Input:
  74. ;    AH = character to search for
  75. ;    CX = maximum length to be searched (must be > 0)
  76. ;    DS:SI = pointer to buffer to be searched
  77. ;
  78. ; Output:
  79. ;    CX = 0 if and only if we ran out of bytes without finding
  80. ;        either the desired byte or a zero byte
  81. ;    DS:SI = pointer to searched-for byte if found, otherwise byte
  82. ;        after zero byte if found, otherwise byte after last
  83. ;        byte checked if neither searched-for byte nor zero
  84. ;        byte is found
  85. ;    Carry Flag = set if searched-for byte found, reset otherwise
  86.  
  87. SearchMaxLength    proc    near
  88.     cld
  89.     mov    bx,cx
  90.     add    cx,3        ;calculate the maximum # of passes
  91.     shr    cx,1        ; through the loop, which is
  92.     shr    cx,1        ; unrolled 4 times
  93.     and    bx,3        ;calculate the index into the entry
  94.                 ; point table for the first,
  95.                 ; possibly partial loop
  96.     shl    bx,1        ;prepare for a word-sized look-up
  97.     jmp    SearchMaxLengthEntryTable[bx]
  98.                 ;branch into the unrolled loop to do
  99.                 ; the first, possibly partial loop
  100.  
  101. SearchMaxLengthLoop:
  102. SearchMaxLengthEntry4:
  103.     lodsb            ;get the next byte
  104.     cmp    al,ah        ;is this the byte we want?
  105.     jz    ByteFound    ;yes, we're done with success
  106.     and    al,al        ;is this the terminating 0 byte?
  107.     jz    ByteNotFound    ;yes, we're done with failure
  108. SearchMaxLengthEntry3:
  109.     lodsb            ;get the next byte
  110.     cmp    al,ah        ;is this the byte we want?
  111.     jz    ByteFound    ;yes, we're done with success
  112.     and    al,al        ;is this the terminating 0 byte?
  113.     jz    ByteNotFound    ;yes, we're done with failure
  114. SearchMaxLengthEntry2:
  115.     lodsb            ;get the next byte
  116.     cmp    al,ah        ;is this the byte we want?
  117.     jz    ByteFound    ;yes, we're done with success
  118.     and    al,al        ;is this the terminating 0 byte?
  119.     jz    ByteNotFound    ;yes, we're done with failure
  120. SearchMaxLengthEntry1:
  121.     lodsb            ;get the next byte
  122.     cmp    al,ah        ;is this the byte we want?
  123.     jz    ByteFound    ;yes, we're done with success
  124.     and    al,al        ;is this the terminating 0 byte?
  125.     jz    ByteNotFound    ;yes, we're done with failure
  126.     loop    SearchMaxLengthLoop ;it's neither, so check the next
  127.                 ; four bytes, if any
  128. ByteNotFound:
  129.     clc            ;return "not found" status
  130.     ret
  131. ByteFound:
  132.     dec    si        ;point back to the location at which
  133.                 ; we found the searched-for byte
  134.     stc            ;return "found" status
  135.     ret
  136. SearchMaxLength    endp
  137.     end    Start
  138.  
  139.